// pages/book/index.js
var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addFrom: '',
    addTo: '',
    dis: 0,
    carInfo: '',
    disPrice: 0,
    floorData: ['无电梯一层', '无电梯二层', '无电梯三层', '无电梯四层', '无电梯五层', '无电梯六层', '无电梯七层'],
    floorPriceArr: [],
    popBoxShow: true,
    floorInfoA: '无电梯一层',
    floorInfoB: '无电梯一层',
    floorPriceA: 0,
    floorPriceB: 0,
    totalPrice: 0,
    floorType: 1,
    wpTotalPrice: 0,
    wpQty: 0,
    placeQty: 0,
    shiftQty: 20,
    placeFee: 0,
    shiftFee: 0,
    floorPrice: 0,
    totalFloorPrice: 0,
    carType: '0',
    carIypeInfo: '小型车(0.6-0.8吨)',
    carIntro: '适用于没有家具电器的微型搬家',
    price: '260',
    addFrom: '',
    addTo: '',
    latFrom: '',
    lngFrom: '',
    latTo: '',
    lngTo: '',
    dis: '0',
    addType: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  //距离费
  calDisPrice: function (n) {
    return (n - 10) > 0 ? (5 * (n - 10)).toFixed(1) : 0
  },
  //平面搬运费
  calShiftPrice: function (n) {
    if(typeof n == 'number'){
      return (n - 20) > 0 ? n - 20 : 0
    }else{
      if(n.detail.value>20){
        this.setData({
          shiftFee: n.detail.value - 20
        })
      }else{
        this.setData({
          shiftFee: 0
        })
      }
      this.data.shiftQty = n.detail.value
    }
    this.calTotal()
  },
 
  carSelect: function () {
    var self = this;
    wx.showActionSheet({
      itemList: ['小型车(0.6-0.8吨)', '中型车(1吨封闭货车)', '中型车(1.5吨敞篷车)', '厢货车(2吨封闭货车)'],
      success: function (res) {
        switch (res.tapIndex) {
          case 0:
            self.setData({
              carIntro: '适用于没有家具电器的微型搬家',
              price: '260',
              carType: '0',
              carIypeInfo: '小型车(0.6-0.8吨)'
            })
            break;
          case 1:
            self.setData({
              carIntro: '适用于少量电器的小型搬家',
              price: '400',
              carType: "1",
              carIypeInfo: '中型车(1吨封闭货车)'
            })
            break;
          case 2:
            self.setData({
              carIntro: '适用于少量电器和家具的中型搬家',
              price: '550',
              carType: "2",
              carIypeInfo: '中型车(1.5吨敞篷车)'
            })
            break;
          case 3:
            self.setData({
              carIntro: '适用于较多电器和家具的家庭及企业搬家',
              price: '1050',
              carType: "3",
              carIypeInfo: '厢货车(2吨封闭货车)'
            })
            break;
        }  
        self.calTotal()   
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })  
  },
  tapAdd: function (e) {
    var self = this;
    this.data.addType = e.target.dataset.type;
    console.log(this.data.addType)
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        if (self.data.addType == 1) {
          self.setData({ 
            latFrom: res.latitude,
            lngFrom: res.longitude,
            addFrom: res.name
          })
        } else {
          self.setData({
            latTo: res.latitude,
            lngTo: res.longitude,
            addTo: res.name
          })
        }
        if (self.data.latFrom != '' && self.data.latTo != '') {
          self.calDis()
        }
      }
    })
  },
  //装卸地选择
  changePlaceQty: function (e) {
    var qty = parseInt(this.data.placeQty ); 
    if (e.target.dataset.type == 1) {
      if (qty <= 0) {
        this.setData({
          placeQty: 0
        })
      } else {
        this.setData({
          placeQty: qty - 1
        })
      }
    } else {
      this.setData({
        placeQty: qty + 1
      })
    }
    this.setData({
      placeFee: this.data.placeQty * 100
    })
    this.calTotal()

  },
  calDis: function () {
    var self = this;

    wx.showLoading({
      title: '计算距离中...',
    })
   
    wx.request({
      url: util.baiduUrl,
      data:{
        origin: util.transPos(self.data.latFrom, self.data.lngFrom),
        destination: util.transPos(self.data.latTo, self.data.lngTo),
        ak:util.baiduKey
      },
      method:'GET',
      success:function(data){
        var distance = ((data.data.result.routes[0].distance) / 1000).toFixed(1)
        self.setData({
          dis: distance,
          disPrice: self.calDisPrice(distance)
        });
        self.calTotal();
        wx.hideLoading()
      },
      fail:function(data){
        wx.hideLoading()
      }
    })
   
  },
  //平面搬运距离选择
  changeShiftQty: function (e) {
    var qty = parseInt(this.data.shiftQty);
    console.log(qty)
    if (e.target.dataset.type == 1) {
      if (qty <= 0) {
        this.setData({
          shiftQty: 0
        })
      } else {
        this.setData({
          shiftQty: qty - 1
        })
      }
    } else {
      this.setData({
        shiftQty: qty + 1
      })
    }
    this.setData({
      shiftFee: this.calShiftPrice(this.data.shiftQty)
    })
    this.calTotal()
  },
  //显示电梯列表
  showFloor: function (e) {
    switch (this.data.carType) {
      case "0":
        this.data.floorPriceArr = [0, 20, 40, 60, 80, 100, 120]
        break;
      case "1":
        this.data.floorPriceArr = [0, 40, 60, 80, 100, 120, 140]
        break;
      case "2":
        this.data.floorPriceArr = [0, 60, 80, 100, 120, 140, 170]
        break;
      case "3":
        this.data.floorPriceArr = [0, 80, 100, 120, 140, 160, 190]
        break;
      default:
        break;
    }
    this.data.floorType = e.target.dataset.type;
    this.setData({
      popBoxShow: false
    })
  },
  //遮罩
  masktap: function () {
    this.setData({
      popBoxShow: true
    })
  },
  //电梯列表点击
  floorListTap: function (e) {
    var type = this.data.floorType;
    var index = e.target.dataset.index;
    if (type == 1) {
      this.setData({
        popBoxShow: true,
        floorInfoA: this.data.floorData[index],
        floorPriceA: this.data.floorPriceArr[index]
      })
    } else {
      this.setData({
        popBoxShow: true,
        floorInfoB: this.data.floorData[index],
        floorPriceB: this.data.floorPriceArr[index]
      })
    }
    this.setData({
      totalFloorPrice: parseInt(this.data.floorPriceA) + parseInt(this.data.floorPriceB)
    })
    this.calTotal()
  },
  //去选择大件物品
  selectM: function () {
    wx.navigateTo({
      url: '/pages/cal/list'
    })
  },
  //计算总价
  calTotal: function () {
    this.setData({
      totalPrice: parseFloat(this.data.disPrice)  + parseInt(this.data.placeFee) + parseInt(this.data.shiftFee) + parseInt(this.data.totalFloorPrice) + parseInt(this.data.wpTotalPrice)+parseInt(this.data.price)
    })
  },
  confirm: function () {
    if(this.data.addFrom == '' || this.data.addTo==''){
      wx.showToast({
        title: '请选择地址',
      });
      return;
    }
    app.globalData.addFrom = this.data.addFrom;
    app.globalData.addTo = this.data.addTo;
    app.globalData.dis = this.data.dis;
    app.globalData.carType = this.data.carIypeInfo;
    app.globalData.totalPrice = this.data.totalPrice;
    app.globalData.fromIndex = 1;
    app.globalData.carInfo = this.data.carInfo,
    app.globalData.carPrice = this.data.price
    wx.switchTab({
      url: '/pages/book/book',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


})